package com.nwpu.am.service.impl;

import com.nwpu.am.mapper.OrderMapper;
import com.nwpu.am.pojo.dto.OrderCreateDTO;
import com.nwpu.am.pojo.entity.Order;
import com.nwpu.am.pojo.entity.User;
import com.nwpu.am.service.OrderService;
import com.nwpu.am.utils.id_generator.SnowflakeIdGenerator;
import com.nwpu.am.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderMapper orderMapper;
    @Resource
    private SnowflakeIdGenerator snowflakeIdGenerator;
    public void createOrder(OrderCreateDTO orderCreateDTO) {
        User user = ThreadLocalUtil.get();
        long userId = user.getUserId();
        long orderId = snowflakeIdGenerator.nextId();
        Order order=new Order(orderId, userId, orderCreateDTO.getStatus(), orderCreateDTO.getPayMoney(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),null , orderCreateDTO.getDescription(), orderCreateDTO.getOwnerName(), orderCreateDTO.getOwnerTel());
        orderMapper.insertOrder(order);

    }

    @Override
    public ArrayList<Order> display() {
        return orderMapper.selectAll();
    }
}
